package com.kungfuhacking.wristbandpro.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.audio.MediaRecordManager;
import com.kungfuhacking.wristbandpro.mqtt.bean.VideoBean;

import org.greenrobot.eventbus.EventBus;


/**
 * 自定义的录音view
 * Created by Aaron on 15/12/24.
 */
public class RecorderTextView extends Button {

    private float pressY;//按下时的y点坐标
    //    private float pressY;
    private boolean isCancle;//是否取消发送

    //    private long currentTime;//当前的时间
    private float chageY;//
    //    private float moveY;
    private final long CHANGE_Y = 50;

    public static final int ACTION_NORMAL = 0x1;
    public static final int ACTION_DOWN = 0x72;//手指按下,开始录音
    public static final int ACTION_MOVE_SEND = 0x73;//手指移动,没有移动到一定距离,可以发送
    public static final int ACTION_MOVE_CANCLE = 0x74;//手指移动距离大,取消发送
    public static final int ACTION_UP = 0x75;//手指抬起
    public static final int ACTION_UP_SHORT_TIME = 0x76;//说话时间过短,抬起
    private long speakTime;
    private Context ctx;
    private String TAG = getClass().getSimpleName();

    public RecorderTextView(Context context) {
        this(context, null);
    }

    public RecorderTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RecorderTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.ctx = context;
        isCancle = false;
        setText(getResources().getString(R.string.press_speak));

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

//        Log.d(TAG, "event.getAction()==" + event.getAction());
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN://按下

                pressY = event.getY();

                EventBus.getDefault().post(ACTION_DOWN);

//                Log.d(TAG, "========ACTION_DOWN" + pressY);
//                pressY = getY();

                speakTime = System.currentTimeMillis();

                setText(getResources().getString(R.string.release_done));

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //开始录音
                        try {
                            MediaRecordManager.getInstance().startRecordAndFile(getContext());
                        } catch (Exception e) {
                            e.printStackTrace();
                            Log.d(TAG, "没有外部存储设备");
                        }
                    }
                }).start();

                break;

            case MotionEvent.ACTION_MOVE://手指移动

//                currentTime = new Date().getTime();

//                moveY = event.getY();
//                float moveY = getY();

                chageY = Math.abs(event.getY() - pressY);

//                Log.d(TAG, "========ACTION_MOVE" + chageY);

                if (chageY > CHANGE_Y) {
                    //滑动的Y轴方向大于50像素
                    isCancle = true;
                    EventBus.getDefault().post(ACTION_MOVE_CANCLE);//通知 页面取消发送语音

                } else {
                    isCancle = false;
                    EventBus.getDefault().post(ACTION_MOVE_SEND);//通知 页面发送语音
                }

                break;

            case MotionEvent.ACTION_UP:

                if ((System.currentTimeMillis() - speakTime) < 900) {//说话时间太短
                    EventBus.getDefault().post(ACTION_UP_SHORT_TIME);
                    isCancle = true;
                } else
                    EventBus.getDefault().post(ACTION_UP);

                //停止录音
                MediaRecordManager.getInstance().stopRecordAndFile();
                VideoBean videoBean = new VideoBean();
                videoBean.isCancle = isCancle;
//                videoBean.millisecond = System.currentTimeMillis() - speakTime;
                if (isCancle) {
                    //取消发送
                    EventBus.getDefault().post(videoBean);
                    //删除该条录音
                    MediaRecordManager.getInstance().deleteMediaRecord();

                } else
                    //发送
                    EventBus.getDefault().post(videoBean);

                break;

        }

        return true;
    }

}
